package dataStructs.tree;

/**
 * @author Jonny Long
 * @date 2021/3/9 8:12
 */
public class ThreadedBinaryTree {
    private ThreadedNode root;
    private ThreadedNode preNode;

    public void setRoot(ThreadedNode root) {
        this.root = root;
    }

    public ThreadedNode getRoot() {
        return root;
    }

    // 中序遍历
    public void infixOrder(ThreadedNode current) {
        if (current != null) {
            infixOrder(current.getLeft());
            System.out.println(current);
            infixOrder(current.getRight());
        }
    }

    // 中序线索化二叉树
    public void threadedNodes(ThreadedNode node){
        if (node==null){
            return;
        }
        threadedNodes(node.getLeft());
        if (node.getLeft()==null){
            node.setLeft(preNode);
            node.setLeftType(true);
        }
        if (preNode != null && preNode.getRight() == null) {
            preNode.setRight(node);
            preNode.setRightType(true);
        }
        // 每处理一个结点后，让当前结点是下一个结点的前驱结点
        preNode = node;
        threadedNodes(node.getRight());
    }

    // 遍历线索化二叉树
    public void threadedList(){
        // 定义移动指针p
        ThreadedNode p = root;
        while (p!=null){
            while (p.isLeftType()==false){
                p=p.getLeft();
            }
            System.out.println(p);
            while (p.isRightType()==true){
                p=p.getRight();
                System.out.println(p);
            }
            // 替换遍历的结点
            p=p.getRight();
        }
    }
}
